﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.Utils;
using Ccit.OA.MsPersonDoc;
using Newtonsoft.Json;
using System.Xml;

namespace Ccit.OA.PersonDoc
{
    public partial class persondoc_mydoc_searchlist : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ViewState["sParaXml"] = (null != Request["sParaXml"]) ? Request["sParaXml"].ToString() : "";
                hidFoldID.Value = (null != Request.QueryString["folderID"]) ? Request.QueryString["folderID"].ToString() : "";
                hidUserId.Value = (null != Request.QueryString["UserIdShare"]) ? Request.QueryString["UserIdShare"].ToString() : "";
                InitPage();
            }
            BindGridView();
        }

        private void InitPage()
        {
            LoginUser loginUser = Session["LogUserInfo"] as LoginUser;

            string userId = hidUserId.Value;

            /*文件夹所属用户  ""为当前人自己,非空时为共享人*/
            if (hidUserId.Value.Trim() == "")
            {
                hidUserId.Value = loginUser.UserId;
            }
        }

        private void BindGridView()
        {
            dgMain.DataSource = GetDateBind();
            dgMain.DataBind();
        }

        private DataSet GetDateBind()
        {
            string sParaXml = ViewState["sParaXml"].ToString().Trim();

            DataSet dSet = new DataSet();

            LoginUser loginUser = Session["LogUserInfo"] as LoginUser;
            PersonDocBLL bll = new PersonDocBLL();
            if (hidFoldID.Value != "")  {
                MsFiles Ms = new MsFiles();

                Ms.folderId = hidFoldID.Value.Trim();
                Ms.userId = hidUserId.Value;
                dSet = bll.GetFilesMess(Ms, "");
            }
            else { 
                dSet = bll.GetFilesSearch(sParaXml);
            }

            bll.Release();
            bll = null;
            return dSet;
        }

        protected void dgMain_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
        {
            if (e.RowType != DevExpress.Web.ASPxGridView.GridViewRowType.Data) return;
            #region 更新列值

            PersonDocBLL bll = new PersonDocBLL();
            string ID = dgMain.GetRowValues(e.VisibleIndex, "ID").ToString().Trim();

            string fileTitle = dgMain.GetRowValues(e.VisibleIndex, "Title").ToString().Trim();
            string fileType = dgMain.GetRowValues(e.VisibleIndex, "FileType").ToString().Trim();
            string fileLength = dgMain.GetRowValues(e.VisibleIndex, "Length").ToString().Trim();
            e.Row.Cells[1].Text = "<img src='../images/filetype/" + bll.GetTypeimage(fileType) + "' border='0'>";
            e.Row.Cells[2].Text = fileTitle;
            if (fileLength != "")
            {
                e.Row.Cells[4].Text = (double.Parse(fileLength) / 1024).ToString("F2").Trim() + "KB";
            }
            //e.Row.Cells[4].Text = bll.GetTypetxt(fileType);
            #endregion

            #region 操作列按钮

          //  LinkButton lnbEdit = (LinkButton)dgMain.FindRowCellTemplateControl(e.VisibleIndex, null, "lnbEdit");
          //  LinkButton lnbDel = (LinkButton)this.dgMain.FindRowCellTemplateControl(e.VisibleIndex, null, "lnbDel");

            string encodeparam = string.Empty;
            if (fileType == "")
            {
                encodeparam = Base64Lib.Encode("folderID=" + ID + "&folderType=" + hidfolderType.Value.Trim() + "&Share=" + hidShareType.Value + "&UserIdShare=" + hidUserId.Value.Trim());
                e.Row.Cells[2].Text = "<a href=\"javascript:doLocationFolder('" + encodeparam + "');\">" + fileTitle + "</a>";
            //    lnbDel.Visible = false;
            }
            else
            {
                encodeparam = Base64Lib.Encode("Field=" + ID + "&folderType=" + hidfolderType.Value.Trim() + "&UserIdShare=" + hidUserId.Value.Trim());
               // lnbEdit.Visible = false;
                e.Row.Cells[2].Text = "<a href=\"javascript:OpenUpLoadPage('" + encodeparam + "');\">" + fileTitle + "</a>";
            }

            if (hidShareType.Value != "00")
            {
             //   lnbEdit.Enabled = false;
             //   lnbDel.Enabled = false;
            }
            else
            {

              //  lnbDel.Attributes["onclick"] = "javascript:doDelete('" + ID + "','" + fileType + "');return false;";
              //  lnbEdit.Attributes["onclick"] = "javascript:return doEditFolder('" + Base64Lib.Encode("type=1&FId=" + ID) + "');";
            }
            #endregion

            bll.Release();
            bll = null;
        }

        /// <summary>
        /// 删除ajax方法 删除文件
        /// </summary>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string DeleteFile(string serExpr, string operType)
        {
            MsFiles ms = (MsFiles)JavaScriptConvert.DeserializeObject(serExpr, typeof(MsFiles));
            MsFolder msfolder = new MsFolder();
            PersonDocBLL bll = new PersonDocBLL();
            msfolder = bll.DelFiles(ms);
            var newObj = new { operType = operType, resultSign = "1", resultDelId = msfolder.FId, resultTypeId = ms.fileType };
            string resultSerExpr = JavaScriptConvert.SerializeObject(newObj);

            bll.Release();
            bll = null;
            ms = null;
            return resultSerExpr;
        }


    }
}
